package com.dp.service.impl3;


import java.util.ArrayList;
import java.util.StringTokenizer;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import com.dp.util.BaseBean;
import com.dp.util.DataMap;
import com.dp.util.PageBean;
@Service
@Transactional
public   class ChannelBeanImpl extends BaseImpl{
	private static Logger log = Logger.getLogger(ChannelBeanImpl.class);
//	public PageBean myChannelList(DataMap hashMap) {
//		int  level			=	hashMap.getMapint("level");
//		String sql = " SELECT tc.channel_id,tc.service_code,tc.channel_name,tc.channel_comment,tc.function_name,tc.status,tc.ordered,tc.created_date,tc.previous_id,tc.primal_id,tc.level,tc.channel_type,tc.channel_hot_topics,tc.channel_classifying_label" +
//				" FROM t_channels as t1 " ;
//		String channel_name=hashMap.getMapData("channel_name");		
//		String employee_id=hashMap.getMapData("employee_id");		
//		String status=hashMap.getMapData("status");		
//		String function_name=hashMap.getMapData("function_name");	
//		
//		String role_id = hashMap.getMapData("role_id");
//		String user_id = hashMap.getMapData("user_id");
//		if(StringIsNull(role_id) && !user_id.equals(""))
//		{
//			sql = sql+ ", t_user_channels as t2 WHERE t1.channel_id=t2.channel_id";
//			if(StringIsNull(employee_id))
//			{
//				sql=sql+" and t2.employee_id="+ObjectToStringDB(employee_id);
//			}
//		}
//		else
//		{
//			sql = sql+ " where 1=1 ";
//		}
//		
//		if(StringIsNull(function_name))
//		{
//			sql=sql+" and t1.function_name="+ObjectToStringDB(function_name);
//		}
//		if(StringIsNull(channel_name))
//		{
//			sql=sql+" and t1.channel_name like '%"+channel_name+"%'";
//		}
//		if(StringIsNull(status))
//		{
//			sql=sql+" and status="+ObjectToStringDB(status);
//		}
//		sql = sql + " order by created_date desc";
//		log.debug("sql="+sql);
//		try {
//			hashMap.put("sql", sql);
//
//			return new PageBean(hashMap);
//		} catch (Exception e) {
//			log.info(" myChannelList fail !!!" +e.getMessage());
//			return null;
//		}
//	}
	public PageBean getChannels(DataMap hashMap) {

//		CHANNELSONG
//
//		LAST
//
//		FIRST
//
//		NORMAL
		// ENABLED
		// DISABLED
		String employee_id = hashMap.getMapData("employee_id");
		String user_id = hashMap.getMapData("user_id");
		String role_id = hashMap.getMapData("role_id");
		String sql = "SELECT  tc.channel_id  ,tc.service_code,tc.channel_name,tc.channel_comment ,tc.function_name,tc.STATUS ,tc.ordered ,tc.created_date,tc.previous_id ,tc.primal_id ,tc.LEVEL ,tc.channel_type  ,tc.channel_hot_topics ,tc.channel_classifying_label" +"  FROM t_channels tc ";
		int  level			=	hashMap.getMapint("level");
		if(StringIsNull(role_id) && !user_id.equals(""))
		{
			sql=sql+",t_user_channels tuc where tuc.employee_id = '"+employee_id+"' and tuc.channel_id = tc.channel_id";
		}
		else
		{			
			sql = sql + " where 1=1" +"";
//					"level ='"+level+"'";
		}
//		String sql = "SELECT * FROM t_channels as t1 where level ='"+level+"'";	
		String channel_name=hashMap.getMapData("channel_name");		
		String status=hashMap.getMapData("status");		
		String function_name=hashMap.getMapData("function_name");	
		String channel_id=hashMap.getMapData("channel_id");
		
		if(StringIsNull(function_name))
		{
			sql=sql+" and function_name="+ObjectToStringDB(function_name);
		}
		if(level==1)
		{
			sql = sql + " and level ='"+level+"'";
			if(StringIsNull(channel_name))
			{
				sql=sql+" and channel_name like '%"+channel_name+"%'";
			}
		}
		else
		{
			if(StringIsNull(channel_id))
			{
				sql=sql+" and previous_id="+ObjectToStringDB(channel_id) + " and level != 1";
			}
		}
		if(StringIsNull(status))
		{
			sql=sql+" and status="+ObjectToStringDB(status);
		}
		sql = sql + " order by created_date desc";
		log.debug("sql="+sql);
		try {
			hashMap.put("sql", sql);
			return new PageBean(hashMap);
		} catch (Exception e) {
			log.info(" getChannels fail !!!" +e.getMessage());
			return null;
		}
	}
	
	public PageBean priceList(DataMap hashMap){
		String channel_id = hashMap.getMapData("channel_id");
		String channel_name = hashMap.getMapData("channel_name");
		String[] user = (String[])hashMap.get("selected_user");
		String role_id = hashMap.getMapData("role_id");
		String user_id = hashMap.getMapData("user_id");
		String sql = "SELECT distinct p.serviceid,p.servicename,p.service_code,p.price,p.price_type,p.servicetype,p.access_no,p.port_type,p.content,p.content_type,p.processClass,p.carrier,p.spnumber,p.order_success_msg,p.ctrmand_cmd,p.ctrmand_no,p.starttime" +
				" FROM t_service_info p left join " +
				"(select tup.service_code,tu.user_id from t_user tu left join t_user_prices tup on tu.employee_id=tup.employee_id) tups " +
				"on p.service_code=tups.service_code where 1=1 ";
		if(StringIsNotNull(role_id))
		{
			sql = sql + " and tups.user_id='" + user_id +"' "; 
		}
//		String sql = "select distinct t_service_info.*,t_employee.employee_name,t_user.user_id from t_service_info,t_user_prices,t_employee,t_user "+
//		"where t_service_info.service_code = t_user_prices.service_code and t_user_prices.employee_id = t_employee.employee_id and t_employee.employee_id = t_user.employee_id ";
//		if(StringIsNotNull(role_id))
//		{
//			sql = sql + " and t_user.user_id='" + user_id +"' "; 
//		}
//		if(user!=null)
//		{
//			sql=sql+" and t_user.employee_id in (select employee_id from t_user where user_id = '";
//			for(int i=0;i<user.length;i++){
//				String userid = user[i];
//				userid = userid.substring(userid.lastIndexOf("(")+1,userid.lastIndexOf("")-1);
//				if(i<user.length-1){
//					sql += userid+"' or user_id ='";
//				}else{
//					sql += userid+"')";
//				}
//			}
//		}
		sql = sql + " order by service_code";
		hashMap.put("sql", sql);
		return new PageBean(hashMap);
	}
	
//	public PageBean getFinalLevelChannels(DataMap hashMap){
//		String sql = "select * from t_channels where (function_name = 'LAST' or function_name = 'CHANNELSONG') ";
//		String channel_name=hashMap.getMapData("channel_name");
//		String function_name=hashMap.getMapData("function_name");
//		if(StringIsNull(function_name))
//		{
//			sql=sql+" and function_name="+ObjectToStringDB(function_name);
//		}		
//		if(StringIsNull(channel_name))
//		{
//			sql=sql+" and channel_name like '%"+channel_name+"%'";
//		}		
//		sql = sql + " order by created_date desc";
//		try {
//			hashMap.put("sql", sql);
//			return new PageBean(hashMap);
//		} catch (Exception e) {
//			log.info(" getFinalLevelChannels fail !!!" +e.getMessage());
//			return null;
//		}
//	}
	
	public PageBean assignChannel(DataMap hashMap) throws Exception{
		return new PageBean(hashMap);
	}
	
	public   void getUserInfo(HttpServletRequest request) {
		//DBOperate dbAction = this;
		String sql = "select t_employee.employee_name,t_user.user_id from t_employee inner join t_user on t_employee.employee_id = t_user.employee_id";
		ArrayList userlist = null;
		try {
			userlist = this.queryALLResultSet(sql);
		} catch (Exception e) {
			log.info(" getUserInfo fail !!!" +e.getMessage());
			e.printStackTrace();
		}
		String channel_id = (String) request.getAttribute("channel_id");
			sql = "select t_employee.employee_name,t_user.user_id from t_employee ,t_user where t_employee.employee_id in (select employee_id from t_user_channels where channel_id = '"+channel_id+"')  and  t_employee.employee_id = t_user.employee_id";
			ArrayList selecteduserList = null;
		try {
			selecteduserList = this.queryALLResultSet(sql);
		} catch (Exception e) {
			log.info(" getUserInfo fail !!!" +e.getMessage());
			e.printStackTrace();
		}
		sql = "select p.serviceid,p.servicename,p.service_code,p.price,p.price_type,p.servicetype,p.access_no,p.port_type,p.content,p.content_type,p.processClass,p.carrier,p.spnumber,p.order_success_msg,p.ctrmand_cmd,p.ctrmand_no,p.starttime" +
				" from t_service_info p where p.service_code in (select service_code from t_channels where channel_id = '"+channel_id+"')";
		ArrayList serviceInfo  = null;
		try {
			serviceInfo = this.queryALLResultSet(sql);
		} catch (Exception e) {
			log.info(" getUserInfo fail !!!" +e.getMessage());
			e.printStackTrace();
		}
		Object[] obj = userlist.toArray();
		request.setAttribute("userlist", obj);
		obj = selecteduserList.toArray();
		request.setAttribute("selecteduser", obj);
		obj = serviceInfo.toArray();
		request.setAttribute("serviceInfo", obj);
	}
	
//	public PageBean assginPrice(DataMap hashMap) throws Exception{
//		String sql = null;
///*		int level = 0;
//		String channel_name=hashMap.getMapData("channel_name");		
//		String channle_id = hashMap.getMapData("channel_id");
//		
//		sql = "select level from t_channels where channel_id = '"+channle_id+"'";
//		DBOperate dbAction = this;
//		ArrayList list = dbAction.queryALLResultSet(sql);
//		level = Integer.parseInt((String)(list.toArray()[0]));*/
//		
//		sql = "select p.serviceid,p.servicename,p.service_code,p.price,p.price_type,p.servicetype,p.access_no,p.port_type,p.content,p.content_type,p.processClass,p.carrier,p.spnumber,p.order_success_msg,p.ctrmand_cmd,p.ctrmand_no,p.starttime" +
//				" ,t_employee.employee_name,t_user.user_id from t_service_info p,t_employee,t_user " +
//				"where t_employee.employee_id = t_user.employee_id and t_employee.employee_id in " +
//				"(select employee_id from t_user_prices where service_code = p.service_code) order by service_code";
//		hashMap.put("sql", sql);
//		return new PageBean(hashMap);
//	}
	
//	public   boolean addChannel(DataMap hashMap) {
//		String channel_id		=	ObjectToStringDB(hashMap.getMapData("new_channel_id"));	
//		String channel_name		=	ObjectToStringDB(hashMap.getMapData("new_channel_name"));
//		String channel_comment		=	ObjectToStringDB(hashMap.getMapData("new_channel_comment"));
//		String function_name	=	ObjectToStringDB(hashMap.getMapData("new_function_name"));	
//		String channel_type	=	ObjectToStringDB(hashMap.getMapData("new_channel_type"));	
//		String channel_classifying_label	=	ObjectToStringDB(hashMap.getMapData("channel_classifying_label"));	
//		String channel_hot_topics	=	ObjectToStringDB(hashMap.getMapData("channel_hot_topics"));	
//		String level			=	ObjectToStringDB(hashMap.getMapint("level"));	
//		String primal_id		=	ObjectToStringDB(hashMap.getMapData("primal_id"));	
//		String previous_id		=	ObjectToStringDB(hashMap.getMapData("previous_id"));	
//		String created_date		=	ObjectToStringDB(getNowTime());	
//		String status			=	"'ENABLED'";
//		int ordered				=	hashMap.getMapint("ordered");	
//		String querySql = "insert into t_channels " +
//				"(channel_id,channel_name,channel_comment,function_name,channel_type,channel_classifying_label,channel_hot_topics,status,ordered,created_date,previous_id,primal_id,level)" +
//				" values (" +
//				channel_id+"," +
//				channel_name+"," +
//				channel_comment+"," +
//				function_name+"," +
//				channel_type+"," +
//				channel_classifying_label+"," +
//				channel_hot_topics+"," +
//				status+"," +
//				ordered+"," +
//				created_date+"," +
//				previous_id+"," +
//				primal_id+"," +
//				level+")";
//		log.debug("addChannel sql: "+querySql);
//		   try {this.updateNativeQuery(querySql);
//				return true;
//			} catch (Exception e) {
//				log.info(" addChannel fail !!!" +e.getMessage());
//				return false;
//			}
//	}
//	public   boolean makeData()
//	{
//		String sql = "call makedata(); ";
//		try {
//			return true;
//		//	return this.execCall(sql);
//		} catch (Exception e) {
//			e.printStackTrace();
//			return false;
//		}
//	}
//	public boolean checkName(DataMap hashMap) {
//		String channel_name = ObjectToString(hashMap.getMapData("new_channel_name"));	
//		if(channel_name.toLowerCase().equals("news"))
//			return false;
//		else
//			return true;
////		String querySql = "SELECT * FROM t_channels as t1 where channel_name = " + channel_name;
////		log.debug("querySql="+querySql);
////		try {
////			DataMap map=this.queryOneRow(querySql);
////			if(map.size()>0)
////			{
////				return true;
////			}
////		} catch (Exception e) {
////			log.info(" checkName fail !!!" +e.getMessage());
////		}
////		return false;
//	}
	public   boolean checkHasLast(DataMap hashMap) {
		int  level			=	hashMap.getMapint("level");
		sql= "SELECT count(*) FROM t_channels as t1 where function_name in ('CHANNELSONG','LAST') " +
				"and  previous_id=" +ObjectToStringDB(hashMap.getMapData("channel_id"));
		log.debug("querySql="+sql);
		try {
//			DataMap map=this.queryOneRow(querySql);
//			if(map.size()>0)
//			{
//				return true;
//			}
			return this.getOneInt()>0;
		} catch (Exception e) {
			log.info(" checkHasLast fail !!!" +e.getMessage());
		}
		return false;
	}
//	public  boolean checkHasNormal(DataMap hashMap) {
//		int  level			=	hashMap.getMapint("level");
//		sql = "SELECT coun(*) FROM t_channels as t1 where function_name in ('NORMAL') " +
//				"and  previous_id=" +ObjectToStringDB(hashMap.getMapData("channel_id"));
//		log.debug("querySql="+sql);
//		try {
////			DataMap map=this.queryOneRow(querySql);
////			if(map.size()>0)
////			{
////				return true;
////			}
//			return this.getOneInt()>0;
//		} catch (Exception e) {
//			log.info(" checkHasNormal fail !!!" +e.getMessage());
//		}
//		return false;
//	}
	//phase2
	public PageBean getChannelsByNews(DataMap hashMap) {
		String employee_id = hashMap.getMapData("employee_id");
		String user_id = hashMap.getMapData("user_id");
		String role_id = hashMap.getMapData("role_id");
		String channel_name=hashMap.getMapData("channel_name");		
		String channel_id=hashMap.getMapData("channel_id");
		String sql = "SELECT tc.channel_id,tc.service_code,tc.channel_name,tc.channel_comment,tc.function_name,tc.status,tc.ordered,tc.created_date,tc.previous_id,tc.primal_id,tc.level,tc.channel_type,tc.channel_hot_topics,tc.channel_classifying_label" +
				",(case when nf.service_code is null then tc.service_code else nf.service_code end ) as service_code FROM " +
				"t_channels tc left join news_extends_firstchannel nf on nf.channel_id=tc.channel_id ";
		int level =	hashMap.getMapint("level");
		if(level == 2)
		{
			sql=sql+" where previous_id =(select channel_id from t_channels where channel_name='"+channel_name+"' and status='ENABLED') and level != 1 ";
		}
		else
		{			
			if(StringIsNull(channel_id))
			{
				sql=sql+" where previous_id="+ObjectToStringDB(channel_id);
			}
		}
		log.debug("getChannelsByNews sql="+sql);
		try {
			hashMap.put("sql", sql);
			return new PageBean(hashMap);
		} catch (Exception e) {
			log.info(" getChannelsByNews fail !!!" +e.getMessage());
			return null;
		}
	}
//	public DataMap getCurrentIcon(String channel_id) {
//		String querySql = "select nr.rid,nr.useragents,nr.name,nr.resourcetype,nr.url,nr.size,nr.rformat,nr.distributorid,nr.spcode,nr.isusebysys,nr.newformat,nr.fullpath" +
//				" from news_resources nr where rid = (SELECT iconid FROM news_extends_channel where channel_id='"+channel_id+"')";
//		DataMap a = null;
//		try {
//			//a= this.queryOneRow(querySql);
//			return a;
//		} catch (Exception e) {
//			log.info(" getCurrentIcon fail !!!" +e.getMessage());
//			return null;
//		}
//	}
//	public DataMap getCurrentTimer(String channel_id) {
//	
//		try {
//			String updatetimer = this.getOneString("SELECT updatetimer FROM news_extends_channel where channel_id='"+channel_id+"'");
//			String querySql = "";
//			DataMap a = new DataMap();
//			if(updatetimer != null && !updatetimer.equals("0"))
//			{
//				querySql = "select tsa.* from t_sys_autoactivetimer tsa where timerid = (SELECT updatetimer FROM news_extends_channel where channel_id='"+channel_id+"')";
//				//a = db.queryOneRow(querySql);
//			}
//			if(updatetimer != null && updatetimer.equals("0"))
//			{
//				log.info(" updatetimer = 0");
//				a.put("timerid", "0");
//			}
//			return a;
//		} catch (Exception e) {
//			log.info(" getCurrentIcon fail !!!" +e.getMessage());
//			return null;
//		}
//	}
	public String getCurrentChannelType(String channel_id) {
		String querySql = "SELECT channeltype FROM news_extends_firstchannel where channel_id='"+channel_id+"'";
		try {
			return this.queryOne(querySql);
		} catch (Exception e) {
			log.info(" getCurrentChannelType fail !!!" +e.getMessage());
			return null;
		}
	}
	public ArrayList getIconList() {
		String querySql = "SELECT nr.rid,nr.useragents,nr.name,nr.resourcetype,nr.url,nr.size,nr.rformat,nr.distributorid,nr.spcode,nr.isusebysys,nr.newformat,nr.fullpath" +
				" FROM news_resources nr where nr.isusebysys=1 and nr.resourcetype=1";
		try {
			return this.queryALLResultSet(querySql);
		} catch (Exception e) {
			log.info(" getCurrentIcon fail !!!" +e.getMessage());
			return null;
		}
	}
	public ArrayList getTimerList() {
		String querySql = "SELECT t.timerid,t.timertime,t.timermsg,t.timequeue,t.isvalid FROM t_sys_autoactivetimer t ";
		try {
			return this.queryALLResultSet(querySql);
		} catch (Exception e) {
			log.info(" getCurrentIcon fail !!!" +e.getMessage());
			return null;
		}
	}
	//add 1204 get news_extends_channel
//	public DataMap getExtendsChannel(String channel_id) {
//		String querySql = "SELECT * FROM news_extends_channel as t1 where channel_id="+ObjectToStringDB(channel_id);
//		try {
//			return null;
//			//return this.queryOneRow(querySql);
//		} catch (Exception e) {
//			log.info(" getExtendsChannel fail !!!" +e.getMessage());
//			return null;
//		}
//	}
	
	public boolean updateChannelIcon(DataMap hashMap) {
		String querySql = "update news_extends_channel set iconid="+ObjectToStringDB(hashMap.getMapData("iconid"))+
				",updatetimer="+ObjectToStringDB(hashMap.getMapData("updatetimer"))+ 
				",isshowtime="+ObjectToStringDB(hashMap.getMapData("isshowtime"))+
				",howshowtime="+ObjectToStringDB(hashMap.getMapData("howshowtime"))+
				",whichtag='"+hashMap.getMapData("whichtag")+"'"+
				",isshowshort="+ObjectToStringDB(hashMap.getMapData("isshowshort"))+
				",isshowicon="+ObjectToStringDB(hashMap.getMapData("isshowicon"))+
				",titlenumber="+ObjectToStringDB(hashMap.getMapData("titlenumber"))+
				",channelurl="+ObjectToStringDB(hashMap.getMapData("channelurl"))+
				" where channel_id="+ObjectToStringDB(hashMap.getMapData("channel_id"));
		try {
			log.debug("updateChannelIcon sql: "+querySql);
			this.updateNativeQuery(querySql);
			return true;
		} catch (Exception e) {
			log.info(" updateChannelIcon fail !!!" +e.getMessage());
			return false;
		}
	}
	//not use anymore
	public boolean updateChannelTimer(DataMap hashMap) {
		String querySql = "update news_extends_channel set updatetimer="+ObjectToStringDB(hashMap.getMapData("updatetimer"))+" " +
				"where channel_id="+ObjectToStringDB(hashMap.getMapData("channel_id"));
		try {
			log.debug("updateChannelTimer sql: "+querySql);
			 this.updateNativeQuery(querySql);
			return true;
		} catch (Exception e) {
			log.info(" updateChannelIcon fail !!!" +e.getMessage());
			return false;
		}
	}
	public boolean updateChannelType(DataMap hashMap) {
		String querySql = "update news_extends_firstchannel set channeltype="+ObjectToStringDB(hashMap.getMapData("channeltype"))+" " +
				"where channel_id="+ObjectToStringDB(hashMap.getMapData("channel_id"));
		try {
			log.debug("updateChannelType sql: "+querySql);
			 this.updateNativeQuery(querySql);
				return true;
		} catch (Exception e) {
			log.info(" updateChannelType fail !!!" +e.getMessage());
			return false;
		}
	}
	public ArrayList getChildItems(DataMap dataMap) {
		String channel_id = dataMap.getMapData("channel_id");
		
    	String function_name = dataMap.getMapData("function_name");
    	String tmpSql = "";
    	int result = 0;
    	String querySql = "";
    	ArrayList items = null;
    	try {
	    	if(function_name.toUpperCase().equals("NORMAL") && channel_id!=null)
	    	{
	    		//判断是否有最后一�?
	    		tmpSql = "SELECT count(*) FROM news_extends_firstchannel_channels where channelid=" +
	    				"(SELECT nf.channelid FROM news_extends_firstchannel nf where nf.channel_id='"+channel_id+"')";
				result =this.getOneInt();
				if(result>0)//有最后一�?列最后一�?关联最后一级栏目表 普通栏目表 栏目信息�?ch_10
				{
					querySql = "SELECT nec.channleid as itemid,tc.channel_name as itemname,nfc.channelorder as itemorder FROM " +
							"news_extends_firstchannel_channels nfc,news_extends_channel nec,t_channels tc " +
							"where nfc.channelid=(SELECT channelid FROM news_extends_firstchannel where  channel_id='"+channel_id+"') and " +
									"nec.channel_id=tc.channel_id and nec.channleid=nfc.channleid order by " +
									"(case when nfc.channelorder is null then 0 else nfc.channelorder end)";
					items = this.queryALLResultSet(querySql);
					items.add("last");
					
				}
				else//没有最后一�?列按�?关联按钮�?栏目按钮关联�?ch_36
				{
					querySql = "SELECT nb.buttonid as itemid,nb.buttontext as itemname,nfb.buttonorder as itemorder FROM " +
							"news_extends_firstchannel_buttons nfb,news_buttons nb where nfb.channelid=" +
							"(SELECT channelid FROM news_extends_firstchannel where channel_id='"+channel_id+"') and nb.buttonid=nfb.buttonid " +
									"order by (case when nfb.buttonorder is null then 0 else nfb.buttonorder end)";
					items = this.queryALLResultSet(querySql);
					items.add("normal");
				}
				return items;
	    	}
	    	else if(function_name.toUpperCase().equals("CHANNELSONG")||function_name.toUpperCase().equals("LAST"))//最后一级列内容 ch_21
	    	{
	    		querySql = "SELECT nc.contentid as itemid,nc.contenttitle as itemname,ncc.titleorder as itemorder from " +
	    				"news_extends_channel_title ncc,news_extends_channel nch,news_contenttitle nc where " +
	    				//contentstatus=1 是已审核，contentstatus=5 //广告已审核
	    				"(ncc.contentstatus=1 OR ncc.contentstatus=5) and ncc.istest=1 and " + 
	    				"nch.channel_id='"+channel_id+"' and ncc.channleid=nch.channleid and ncc.cid=nc.cid order by " +
	    						"(case when ncc.titleorder is null then 0 else ncc.titleorder end),nc.cid desc";
	    		items = this.queryALLResultSet(querySql);
	    		items.add("content");
	    		return items;
	    	}
	    	else
	    	{
	    		log.info(" Channel function is not NORMAL or LAST !!!");
	    		return null;
	    	}
	    	
    	} catch (Exception e) {
			log.info(" getChildItems fail !!!" +e.getMessage());
			e.printStackTrace();
			return null;
		}
	}
	public boolean sortChannelItems(DataMap dataMap) {
		String channel_id = dataMap.getMapData("channel_id");
    	String itemtype = dataMap.getMapData("itemtype");
    	StringTokenizer st=new StringTokenizer(dataMap.getMapData("gvalue"),"|");
    	String tmpSql = "";
    	int ordered = 0;
    	ArrayList sqlList = new ArrayList();
    	if(itemtype.equals("last"))//排最后一�?
    	{
    		while(st.hasMoreElements())
    		{
    			ordered=ordered+1;
    			tmpSql = "update news_extends_firstchannel_channels set channelorder="+ordered+" where channleid="+ObjectToStringDB(st.nextElement()) + 
    			" and channelid=(SELECT channelid FROM news_extends_firstchannel where channel_id='"+channel_id+"')";
    			sqlList.add(tmpSql);
    		}
    	}
    	if(itemtype.equals("normal"))//排普通栏目按�?
    	{
    		while(st.hasMoreElements())
    		{
    			ordered=ordered+1;
    			tmpSql = "update news_extends_firstchannel_buttons set buttonorder="+ordered+" where buttonid="+ObjectToStringDB(st.nextElement()) + 
    			" and channelid=(SELECT channelid FROM news_extends_firstchannel where channel_id='"+channel_id+"')";
    			sqlList.add(tmpSql);
    		}
    	}
    	if(itemtype.equals("content"))//排普通栏�?
    	{
    		while(st.hasMoreElements())
    		{
//    			System.out.println(st.nextElement());
    			ordered=ordered+1;
    			tmpSql = "update news_extends_channel_title set titleorder="+ordered+" where cid=(SELECT cid FROM news_contenttitle " +
    				"where contentid="+ObjectToStringDB(st.nextElement())+" and newstype=(select channel_type from t_channels where channel_id='"+channel_id+"')) and channleid=(SELECT channleid FROM news_extends_channel where channel_id='"+channel_id+"')";
    			sqlList.add(tmpSql);
    		}
    	}
    	try {
			return this.execTransaction(sqlList);
		} catch (Exception e) {
			e.printStackTrace();
			log.info(" sortChannelItems fail !!!" +e.getMessage());
			return false;
		}
	}
	public boolean assignPriceChannelNews(DataMap dataMap)
	{
		String service_code = dataMap.getMapData("service_code");
		String channel_id = dataMap.getMapData("channel_id");
		String function_name = dataMap.getMapData("function_name");
		String sql = "";
		if(function_name!=null && (function_name.toUpperCase().equals("LAST") || function_name.toUpperCase().equals("CHANNELSONG")))
			sql = "update t_channels set service_code = '"+service_code+"' where channel_id = '"+channel_id+"'";
		else if(function_name!=null && function_name.toUpperCase().equals("NORMAL"))
			sql = "update news_extends_firstchannel set service_code = '"+service_code+"' where channel_id = '"+channel_id+"'";
		try {
			log.debug("assignPriceChannelNews sql: "+sql);this.updateNativeQuery(sql);
			return true;
		} catch (Exception e) {
			log.info(" assignPriceChannelNews fail !!!" +e.getMessage());
			return false;
		}
	}
//	public DataMap getChannelNews(DataMap hashMap) {
//		String querySql = "SELECT tc.channel_name,tc.function_name,nf.* FROM t_channels tc,news_extends_firstchannel nf where " +
//				"nf.channel_id="+ObjectToStringDB(hashMap.getMapData("channel_id")) + "and tc.channel_id=nf.channel_id";
//		try {
//			return null;//this.queryOneRow(querySql);
//		} catch (Exception e) {
//			log.info(" getChannel fail !!!" +e.getMessage());
//			return null;
//		}
//	}
	public ArrayList getSelectButtons(String channel_id) {
		String querySql = "SELECT nb.buttonid ,nb.buttontext ,nfb.buttonorder FROM " +
		"news_extends_firstchannel_buttons nfb,news_buttons nb where nfb.channelid=" +
		"(SELECT channelid FROM news_extends_firstchannel where channel_id='"+channel_id+"') and nb.buttonid=nfb.buttonid " +
				"order by (case when nfb.buttonorder is null then 99999 else nfb.buttonorder end)";
		try {
			return this.queryALLResultSet(querySql);
		} catch (Exception e) {
			log.info(" getButtons fail !!!" +e.getMessage());
			return null;
		}
	}
//	public ArrayList getAllChButtons() {
//		String querySql = "SELECT * FROM news_buttons n where buttontype=3";
//		try {
//			return this.queryALLResultSet(querySql);
//		} catch (Exception e) {
//			log.info(" get channel Buttons fail !!!" +e.getMessage());
//			return null;
//		}
//	}
//	public ArrayList getAllFunctionButtons() {
//		String querySql = "SELECT * FROM news_buttons n where buttontype=1 or buttontype=2";
//		try {
//			return this.queryALLResultSet(querySql);
//		} catch (Exception e) {
//			log.info(" get function Buttons fail !!!" +e.getMessage());
//			return null;
//		}
//	}
//	public ArrayList getAllPostButtons() {
//		String querySql = "SELECT * FROM news_buttons n where buttontype=4";
//		try {
//			return this.queryALLResultSet(querySql);
//		} catch (Exception e) {
//			log.info(" get post Buttons fail !!!" +e.getMessage());
//			return null;
//		}
//	}
	
	//get ads by channel
	public ArrayList getSelectAdsAtFstCh(String channel_id) {
		String querySql = "SELECT na.adid,na.adtitle,na.adtext FROM news_ads na,news_extends_firstchannel_ads nfa,news_extends_firstchannel nf where " +
				"nf.channel_id='"+channel_id+"' and na.adid=nfa.adid and nfa.channelid=nf.channelid and na.adtype=1";
		try {
			return this.queryALLResultSet(querySql);
		} catch (Exception e) {
			log.info(" getSelectAdsAtFstCh fail !!!" +e.getMessage());
			return null;
		}
	}
	public ArrayList getSelectAdsAtLastCh(String channel_id) {
		String querySql = "SELECT na.adid,na.adtitle, na.adtext FROM news_ads na,news_extends_channel_ads nca,news_extends_channel nc where " +
		"nc.channel_id='"+channel_id+"' and na.adid=nca.adid and nca.channleid=nc.channleid and na.adtype=1";
		try {
			return this.queryALLResultSet(querySql);
		} catch (Exception e) {
			log.info(" getSelectAdsAtLastCh fail !!!" +e.getMessage());
			return null;
		}
	}
	public ArrayList getAllActiveAds() {
		String querySql = "SELECT na.adid,na.adtitle,na.adtext FROM news_ads na where  na.adtype=1";
		try {
			return this.queryALLResultSet(querySql);
		} catch (Exception e) {
			log.info(" getAllActiveAds fail !!!" +e.getMessage());
			return null;
		}
	}
	//set channel ads
	public boolean setChannelAdsAtLastCh(DataMap dataMap,HttpServletRequest request)
	{
		String channel_id = dataMap.getMapData("channel_id");
		String[] selected_ads = request.getParameterValues("selected_ads");
		ArrayList<String> sqlStrList = new ArrayList<String>();
		String sql = "delete from news_extends_channel_ads where channleid=" +
				"(SELECT channleid FROM news_extends_channel where channel_id='"+channel_id+"')";
		sqlStrList.add(sql);
		if (selected_ads != null) {
			for (int i = 0; i < selected_ads.length; i++) {
				String adid = selected_ads[i];
				int adorder = i+1;
				sql = "insert into news_extends_channel_ads values " +
						"((SELECT channleid FROM news_extends_channel where channel_id='"+channel_id+"' )," + adid + "," + adorder + ")";
				sqlStrList.add(sql);
			}
		}
		try {
        	return this.execTransaction(sqlStrList);
        } catch (Exception e) {
        	log.info(" setChannelAdsAtLastCh fail !!!" +e.getMessage());
            return false;
        }
	}
	public boolean setChannelAdsAtFstCh(DataMap dataMap,HttpServletRequest request)
	{
		String channel_id = dataMap.getMapData("channel_id");
		String[] selected_ads = request.getParameterValues("selected_ads");
		ArrayList<String> sqlStrList = new ArrayList<String>();
		String sql = "delete from news_extends_firstchannel_ads where channelid="+
				"(SELECT channelid FROM news_extends_firstchannel where channel_id='"+channel_id+"')";
		sqlStrList.add(sql);
		if (selected_ads != null) {
			for (int i = 0; i < selected_ads.length; i++) {
				String adid = selected_ads[i];
				sql = "insert into news_extends_firstchannel_ads values " +
						"((SELECT channelid FROM news_extends_firstchannel where channel_id='"+channel_id+"' )," + adid + ")";
				sqlStrList.add(sql);
			}
		}
		try {
        	return this.execTransaction(sqlStrList);
        } catch (Exception e) {
        	log.info(" setChannelAdsAtFstCh fail !!!" +e.getMessage());
            return false;
        }
	}
	
	public boolean setChannelBtn(DataMap dataMap,HttpServletRequest request)
	{
		String channel_id = dataMap.getMapData("channel_id");
		String[] selected_btn = request.getParameterValues("selected_btn");
		ArrayList<String> sqlStrList = new ArrayList<String>();
		String sql = "delete from news_extends_firstchannel_buttons where channelid=" +
				"(SELECT channelid FROM news_extends_firstchannel where channel_id='"+channel_id+"')";
		sqlStrList.add(sql);
		int count = 1;
		if (selected_btn != null) {
			for (int i = 0; i < selected_btn.length; i++) {
				String btnid = selected_btn[i];
				int btnorder = i+1;
				sql = "insert into news_extends_firstchannel_buttons values " +
						"((SELECT channelid FROM news_extends_firstchannel where channel_id='"+channel_id+"' )," + btnid + "," + btnorder + ")";
				sqlStrList.add(sql);
				count++;
			}
		}
		String[] sqlStr = sqlStrList.toArray(new String[count]);
		try {
        	boolean bool = this.execTransaction(sqlStr);
            return bool;
        } catch (Exception e) {
        	log.info(" setChannelBtn fail !!!" +e.getMessage());
            return false;
        }
	}
	//----------
//	public DataMap getChannel(DataMap hashMap) {
//		String querySql = "SELECT * FROM t_channels as t1 where channel_id="+ObjectToStringDB(hashMap.getMapData("channel_id"));
//		try {
//			return null;// this.queryOneRow(querySql);
//		} catch (Exception e) {
//			log.info(" getChannel fail !!!" +e.getMessage());
//			return null;
//		}
//	}
	//modify 0222
	public boolean updateChannelName(DataMap hashMap) {
		String ch_name = ObjectToStringDB(hashMap.getMapData("channel_name"));
		String ch_id = ObjectToStringDB(hashMap.getMapData("channel_id"));
		ArrayList<String> sqlList = new ArrayList<String>();
		String querySql = "update t_channels set channel_name="+ch_name+",channel_comment="
			+ObjectToStringDB(hashMap.getMapData("channel_comment"))+",channel_type="+ObjectToStringDB(hashMap.getMapData("channel_type"))+
			",channel_classifying_label="+ObjectToStringDB(hashMap.getMapData("channel_classifying_label"))+"," +
					"channel_hot_topics="+ObjectToStringDB(hashMap.getMapData("channel_hot_topics"))+" where channel_id="+ch_id;
		sqlList.add(querySql);
		String updateSql = "update news_buttons set buttontext="+ch_name+" where buttoncontenttype=(SELECT newscontenttype FROM news_contenttype where channel_id="+ch_id+")";
		sqlList.add(updateSql);
		try {
			log.debug("updateChannelName sql: "+querySql);
			log.debug("updateButtonText sql: "+updateSql);
			return this.execTransaction(sqlList);
		} catch (Exception e) {
			log.info(" updateChannelName fail !!!" +e.getMessage());
			return false;
		}
	}
//	public DataMap getOpeChannel(DataMap hashMap) {
//		String querySql = "SELECT * FROM t_channels as t1 where channel_id="+ObjectToStringDB(hashMap.getMapData("opechannel_id"));
//		try {
//			return null;// this.queryOneRow(querySql);
//		} catch (Exception e) {
//			log.info(" getOpeChannel fail !!!" +e.getMessage());
//			return null;
//		}
//	}
	public boolean updateChannelStatus(DataMap hashMap,String status) 
	{
		String channel_id=hashMap.getMapData("channel_id");
		String ope_channel_id=hashMap.getMapData("opechannel_id");
		ArrayList<String> sqlList = new ArrayList<String>();
		String querySql = "update t_channels set status="+ObjectToStringDB(status)+
			" where channel_id="+ObjectToStringDB(ope_channel_id);
		sqlList.add(querySql);
		if(status.equals("ENABLED"))
			querySql = "insert into news_extends_firstchannel_channels values " +
					"((select channelid from news_extends_firstchannel where channel_id="+ObjectToStringDB(channel_id)+")," +
						"(select channleid from news_extends_channel where channel_id="+ObjectToStringDB(ope_channel_id)+"),0)";
		else
			querySql = "delete from news_extends_firstchannel_channels where channelid=" +
				"(select channelid from news_extends_firstchannel where channel_id="+ObjectToStringDB(channel_id)+") and channleid=" +
				"(select channleid from news_extends_channel where channel_id="+ObjectToStringDB(ope_channel_id)+")";
		sqlList.add(querySql);
		try {
			log.debug("updateChannelStatus sql: "+querySql);
			return this.execTransaction(sqlList);
		} catch (Exception e) {
			log.info(" updateChannelStatus fail !!!" +e.getMessage());
			return false;
		}
	}
	
	private  boolean updateChannelStatusAll(DataMap hashMap,String status) {
		String querySql = "update t_channels set status="+ObjectToStringDB(status)+" " +
				"where primal_id="+ObjectToStringDB(hashMap.getMapData("opechannel_id"));
		try {
			log.debug("updateChannelStatusAll sql: "+querySql); this.updateNativeQuery(querySql);
			return true;
		} catch (Exception e) {
			log.info(" updateChannelStatusAll fail !!!" +e.getMessage());
			return false;
		}
	}
	
	private boolean updateChannelStatusChildren(String opechannel_id,String status) 
	{
		String getChildIdSql = "select channel_id from t_channels where previous_id='"+opechannel_id + "'";
		ArrayList ChildOneId = new ArrayList();
		boolean dbResult = false;
		try {
			ChildOneId = this.queryALLResultSet(getChildIdSql);
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		String querySql = "update t_channels set status="+ObjectToStringDB(status)+"where channel_id='"+opechannel_id + "'";
		try {
			log.debug("updateChannelStatusChildren sql: "+querySql);
			dbResult =true; this.updateNativeQuery(querySql);
			if(ChildOneId.size() > 0)
			{	
				for(int i=0;i<ChildOneId.size();i++)
				{
					DataMap one = (DataMap)ChildOneId.get(i);
					updateChannelStatusChildren(one.getMapData("channel_id"),status);
				}
			}
			return dbResult;
		} catch (Exception e) {
			log.info(" updateChannelStatusChildren fail !!!" +e.getMessage());
			return false;
		}
		
	}
	
	//add 0108
	public ArrayList getSelectChByUser(String user_id) {
		String querySql = "select tuc.channel_id from t_user tu,t_user_channels tuc where tu.user_id='"+user_id+"' and tu.employee_id=tuc.employee_id";
		try {
			return this.queryALLResultSet(querySql);
		} catch (Exception e) {
			log.info(" getSelectChByUser fail !!!" +e.getMessage());
			return null;
		}
	}
//	public ArrayList getAllFirstChs() {
//		String querySql = "SELECT * FROM t_channels where level=2";
//		try {
//			return this.queryALLResultSet(querySql);
//		} catch (Exception e) {
//			log.info(" getAllFirstChs fail !!!" +e.getMessage());
//			return null;
//		}
//	}
	public ArrayList getAllNotFirstChs() {
		//加order by created_date是让channel排序,因为normal和last混合在一起,页面js有可能先处理last
		//而排序后,normal肯定在last之前
		String querySql = "SELECT * FROM t_channels where level>2 order by created_date";
		try {
			return this.queryALLResultSet(querySql);
		} catch (Exception e) {
			log.info(" getAllNotFirstChs fail !!!" +e.getMessage());
			return null;
		}
	}
	public String getEmpidFromUserid(String userId)
	{
		String querySql = "select employee_id from t_user where user_id='"+userId+"'";
		try {
			return this.queryOne(querySql);
		} catch (Exception e) {
			log.info(" getEmpidFromUserid fail !!!" +e.getMessage());
			return null;
		}
	}
	public  boolean assignChannelToUserWithEmpid(String empId,String[] selChidList)
	{
		ArrayList<String> sqlStrList = new ArrayList<String>();
		String delSql = "delete from t_user_channels where employee_id='"+empId+"'";
		sqlStrList.add(delSql);
		String sql = "";
		for (int i = 0; i < selChidList.length; i++) 
		{
			sql = "insert into t_user_channels values ('"+selChidList[i]+"','"+empId+"')";
			sqlStrList.add(sql);
		}
		try {
			return this.execTransaction(sqlStrList);
        } catch (Exception e) {
        	log.info(" assignChannelToUserWithEmpid fail !!!" +e.getMessage());
            return false;
        }
	}
	//add 0205 delete channel
	public   boolean delChannel(DataMap hashMap) 
	{
		String channel_id=hashMap.getMapData("channel_id");
		String channel_name=hashMap.getMapData("opechannel_name");
		String function_name=hashMap.getMapData("function_name");
		String tmpSql = "";
		ArrayList<String> sqlList = new ArrayList<String>();
		if(function_name.equalsIgnoreCase("NORMAL") && channel_id!=null)
    	{
			String buttonname = channel_name+"."+channel_id;
			//删除关联关系
    		tmpSql = "delete from news_extends_firstchannel_buttons where buttonid=" +
    				"(SELECT buttonid FROM news_buttons where buttoncontenttype='"+buttonname+"')";
    		sqlList.add(tmpSql);
    		tmpSql = "delete from news_contenttype where channel_id='"+channel_id+"'";
    		sqlList.add(tmpSql);
    		tmpSql = "delete from t_channels where channel_id='"+channel_id+"'";
    		sqlList.add(tmpSql);
    		tmpSql = "delete from news_extends_firstchannel where channel_id='"+channel_id+"'";
    		sqlList.add(tmpSql);
    		tmpSql = "delete from news_buttons where buttoncontenttype='"+buttonname+"'";
    		sqlList.add(tmpSql);
    	}
    	else if(function_name.toUpperCase().equals("CHANNELSONG")||function_name.toUpperCase().equals("LAST"))
    	{
    		//删除关联关系
    		tmpSql = "delete from news_extends_firstchannel_channels where channleid=" +
    				"(SELECT channleid FROM news_extends_channel where channel_id='"+channel_id+"')";
    		sqlList.add(tmpSql);
    		tmpSql = "delete from t_channels where channel_id='"+channel_id+"'";
    		sqlList.add(tmpSql);
    		tmpSql = "delete from news_extends_channel where channel_id='"+channel_id+"'";
    		sqlList.add(tmpSql);
    	}
		try {
			return this.execTransaction(sqlList);
		} catch (Exception e) {
			log.info(" delChannel fail !!!" +e.getMessage());
			return false;
		}
	}
}
